<?php
namespace app\souther\controller;

use app\common\model\Liuyan as LiuyanModel;
use think\Db;

class Liuyan extends Base
{
	
	function __construct()
	{
		parent::_initialize();
	}


	/**
     * [index  列表]
     * @author [南風]
     * @return mixed|string
     * @throws \Exception
     */
    public function index()
    {
        $model = new LiuyanModel();

        if(input("param.deploy")==1){
            return $this->fetch('deploy',['app'=>'liuyan']);die;
        }

        if(request()->isAjax())
        {
            $data = input('param.');
            $map = [];

            if(is_array($data)){
                if(!empty($data['key']))
                    $map[] = ['name','like',"%" . $data['key'] . "%"];
            }
            $this->getPageAndSize($data);

            try{
                $count =$model->getLiuyanCountByCondition($map);
                $allpage = intval(ceil($count / $this->size));//总页面
                $lists = $model->getLiuyanByCondition($map, $this->from, $this->size);//列表
            }catch (\Exception $e)
            {
                writelog($e->getMessage(),-1);
                exception($e->getMessage());
            }

            if(empty($lists))
                return '';
            
            writelog('查看留言列表');

            return $this->fetch('ajax_list',[
                'count' => $count,//总记录数
                'allpage' => $allpage,
                'lists' => $lists,
            ]);
        }

        return $this->fetch('',['app'=>'liuyan']);
    }

    /**
     * [del  删除]
     * @author [南風]
     * @return array
     */
    public function del()
    {
        $model = new LiuyanModel();

        $id = input('param.id');

        $model->deleteTrueData($id);

        writelog('删除留言--'.$id);

        return admin_json(1,'成功');
    }

    /**
     * [status 状态]
     * @author [南風]
     * @return array
     */
    public function status()
    {
        $model = new LiuyanModel();
        $data=input('param.');
        $res = $model->changeStatus(['id' => $data['id']],$data['status']);

        if($res)
        {
            if($data['status'] == 0)
            {
                writelog('禁用留言状态--'.$data['id']);
                $statusurl = url('status',['id'=>$data['id'],'status'=>1]);
                return admin_json(2,'已禁止',['statusurl'=>$statusurl]);
            }
            else
            {
                writelog('启用留言状态--'.$data['id']);
                $statusurl = url('status',['id'=>$data['id'],'status'=>0]);
                return admin_json(1,'已启用',['statusurl'=>$statusurl]);
            }
        }
        return admin_json(-1,'操作失败');
    }
}